import os
import PySide2
import pandas as pd

dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

from PySide2.QtWidgets import QApplication, QFileDialog, QMessageBox
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QCoreApplication

class Stats:
    def __init__(self):
        # 从文件中加载UI定义

        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意：里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('D:\Code\Python\File2ftr\main.ui')
        loader = QUiLoader()
        # 初始化 gv_visual_data 的显示
        self.ui.StartButton.clicked.connect(self.handleCalc)
        self.ui.QuitButton.clicked.connect(QCoreApplication.quit)


    def handleCalc(self):
        Directory=QFileDialog.getExistingDirectory(self.ui, "请选择文件夹")
        filenames=os.listdir(Directory)
        if self.ui.category.currentText()==".xls":
            XLS=list()
            for filename in filenames:
                if os.path.splitext(filename)[-1]==".xls":
                    XLS.append(filename)
            if len(XLS)!=0:
                for ele in XLS:
                    f=os.path.join(Directory+'\\'+ele)
                    df=pd.read_excel(f)
                    for col in df.columns:
                        w=(df[[col]].applymap(type)!=df[[col]].iloc[0].apply(type)).any(axis=1)
                        if len(df[w]) > 0:
                            df[col] = df[col].astype(str)
                        if df[col].dtype==list:
                            df[col]=df[col].astype(str)
                    pathname = f[:-3] + "ftr"
                    df.to_feather(pathname)
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '转化完毕！')
            else:
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '找不到文件！')
        elif self.ui.category.currentText()==".xlsx":
            XLSX=list()
            for filename in filenames:
                if os.path.splitext(filename)[-1]==".xlsx":
                    XLSX.append(filename)
            if len(XLSX)!=0:
                for ele in XLSX:
                    f=os.path.join(Directory+'\\'+ele)
                    df=pd.read_excel(f)
                    for col in df.columns:
                        w=(df[[col]].applymap(type)!=df[[col]].iloc[0].apply(type)).any(axis=1)
                        if len(df[w]) > 0:
                            df[col] = df[col].astype(str)
                        if df[col].dtype==list:
                            df[col]=df[col].astype(str)
                    pathname = f[:-4] + "ftr"
                    df.to_feather(pathname)
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '转化完毕！')
            else:
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '找不到文件！')
        elif self.ui.category.currentText()==".txt":
            TXT=list()
            for filename in filenames:
                if os.path.splitext(filename)[-1]==".txt":
                    TXT.append(filename)
            if len(TXT)!=0:
                for ele in TXT:
                    f=os.path.join(Directory+'\\'+ele)
                    df=pd.read_table(f,sep='\t')
                    for col in df.columns:
                        w=(df[[col]].applymap(type)!=df[[col]].iloc[0].apply(type)).any(axis=1)
                        if len(df[w]) > 0:
                            df[col] = df[col].astype(str)
                        if df[col].dtype==list:
                            df[col]=df[col].astype(str)
                    pathname = f[:-3] + "ftr"
                    df.to_feather(pathname)
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '转化完毕！')
            else:
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '找不到文件！')
        elif self.ui.category.currentText()==".csv":
            CSV=list()
            for filename in filenames:
                if os.path.splitext(filename)[-1]==".csv":
                    CSV.append(filename)
            if len(CSV)!=0:
                for ele in CSV:
                    f=os.path.join(Directory+'\\'+ele)
                    df=pd.read_csv(f)
                    for col in df.columns:
                        w=(df[[col]].applymap(type)!=df[[col]].iloc[0].apply(type)).any(axis=1)
                        if len(df[w]) > 0:
                            df[col] = df[col].astype(str)
                        if df[col].dtype==list:
                            df[col]=df[col].astype(str)
                    pathname = f[:-3] + "ftr"
                    df.to_feather(pathname)
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '转化完毕！')
            else:
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '找不到文件！')
        elif self.ui.category.currentText()==".tsv":
            TSV=list()
            for filename in filenames:
                if os.path.splitext(filename)[-1]==".tsv":
                    TSV.append(filename)
            if len(TSV)!=0:
                for ele in TSV:
                    f=os.path.join(Directory+'\\'+ele)
                    df=pd.read_csv(f,sep='\t')
                    for col in df.columns:
                        w=(df[[col]].applymap(type)!=df[[col]].iloc[0].apply(type)).any(axis=1)
                        if len(df[w]) > 0:
                            df[col] = df[col].astype(str)
                        if df[col].dtype==list:
                            df[col]=df[col].astype(str)
                    pathname = f[:-3] + "ftr"
                    df.to_feather(pathname)
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '转化完毕！')
            else:
                QMessageBox.about(self.ui,
                        'FTR格式转化器：',
                        '找不到文件！')


app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()
